package com.hyb.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyb.Vo.CustomerDictionaryVo;
import com.hyb.Vo.CustomerVo;
import com.hyb.entity.Customer;
import com.hyb.mapper.CustomerMapper;
import com.hyb.page.PageReq;
import com.hyb.page.VoQuery;
import com.hyb.service.CustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  客户Service接口实现类
 * </p>
 *
 * @author hyb
 * @since 2021-12-29
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public void createCustomerInfo(Customer customer) {
        customerMapper.insert(customer);
    }

    @Override
    public PageReq getCustomerInfo(Integer limit, Integer current) {
        Page<CustomerDictionaryVo> page = new Page<>(current,limit);
        IPage<CustomerDictionaryVo> customerDictionaryVoIPage = customerMapper.selectCustomerInfo(page);
        PageReq pageReq = new PageReq();
        pageReq.setTotal(customerDictionaryVoIPage.getTotal());
        pageReq.setMyData(customerDictionaryVoIPage.getRecords());
        return pageReq;
    }

    @Override
    public CustomerDictionaryVo getCustomerById(Integer customerId) {
        return customerMapper.selectCUstomerById(customerId);
    }

    @Override
    public void updateCustomer(Customer customer) {
        customerMapper.updateById(customer);
    }



    @Override
    public PageReq getDrainCustomers(Integer limit, Integer current, String customerName, String customerState,Integer staffId) {
        Page<CustomerVo> page = new Page<>(current,limit);
        PageReq pageReq = new PageReq();
        VoQuery voQuery = new VoQuery();
        if (!StringUtils.isEmpty(customerName)){
            voQuery.setCustomerName(customerName);
        }
        if (!StringUtils.isEmpty(customerState)){
            voQuery.setCustomerState(customerState);
        }
        voQuery.setStaffId(staffId);

        IPage<CustomerVo> customerVoIPage = customerMapper.selectDrainCustomers(page, voQuery);
        pageReq.setTotal(customerVoIPage.getTotal());
        pageReq.setMyData(customerVoIPage.getRecords());
        return pageReq;
    }

    @Override
    public Integer getToBeConfirmedCustomerNum(Integer staffId) {
        Integer toBeConfirmedCustomerNum = customerMapper.selectToBeConfirmedCustomerNum(staffId);
        return toBeConfirmedCustomerNum;
    }
}
